package com.lsh.linkedList;

/**
 * @author ：LiuShihao
 * @date ：Created in 2020/7/29 3:58 下午
 * @desc ：实现链表增删改查
 */
public class DoubleLinkedListDemo {
    public static void main(String[] args) {
        Hero2 hero1 = new Hero2(1, "宋江", "及时雨");
        Hero2 hero2 =new Hero2(2,"卢俊义","玉麒麟");
        Hero2 hero3 =new Hero2(3,"吴用","智多星");
        Hero2 hero4 =new Hero2(4,"林冲","豹子头");
        Hero2 hero5 =new Hero2(4,"林冲冲","豹子头头");
        DoubleLinkedList doublelinkedList = new DoubleLinkedList();
        //测试   添加
        doublelinkedList.add(hero1);
        doublelinkedList.add(hero2);
        doublelinkedList.add(hero3);
        doublelinkedList.add(hero4);
        //测试  遍历双向列表的元素
        doublelinkedList.show();
        //测试更新
        doublelinkedList.update(hero5);
        System.out.println("-------更新后------");
        doublelinkedList.show();
        //测试删除
        doublelinkedList.del(1);
        doublelinkedList.del(4);
        System.out.println("-------删除后------");
        doublelinkedList.show();


    }
}
class DoubleLinkedList{
    //初始化一个头节点，不存放具体的数据
    private Hero2 head = new Hero2(0,"","");

    public Hero2 getHead() {
        return head;
    }

    public void add(Hero2 hero){
        //不考虑编号顺序时
        //找到当前链表的最后节点，将这个节点的next指向新节点
        //我们需要一个辅助指针
        Hero2 temp = head;
        //遍历链表，找到最后节点
        while(true){
            if (temp.next == null){
                break;
            }
            //如果没有找到最后节点，就将temp往后移
            temp= temp.next;
        }
        //当退出while循环时，temp就指向了链表的最后一个元素
        //将最后元素的next指向新节点即可
        temp.next= hero;
        hero.pre = temp;
    }
    /**
     * 先判断链表是否为空，如果为空直接返回
     * 然后定义一个辅助节点temp，将头节点的next赋值给temp
     * 判断temp的next是否为空 如果为空
     */
    public void show(){
        //判断链表是否为空
        if (head.next == null ){
            System.out.println("链表为空！");
            return;
        }
        Hero2 temp = head.next;
        while(true){
            if (temp == null){
                break;
            }
            //输出节点信息
            System.out.println(temp);
            //将加点后移
            temp = temp.next;
        }
    }
    /**
     * 更新元素
     */
    public void update(Hero2 hero2){
        Hero2 head = this.head;
        if (head.next == null){
            System.out.println("双向链表为空");
            return;
        }
        Hero2 current = head.next;
        Boolean flag = false;
        while(true){
            if (current == null){
                break;
            }
            if (current.no == hero2.no){
                flag = true;
                break;
            }
            current = current.next;
        }
        if (flag){
            current.name = hero2.name;
            current.nickName = hero2.nickName;
        }else {
            System.out.println("双向列表中不存在此英雄，更新失败！");
        }
    }
    /**
     * 删除双向列表中的元素
     * @param no
     */
    public void del(int no){
        Hero2 head = this.head;
        Boolean  flag = false;
        if (head.next == null){
            System.out.println("双向链表为空");
            return;
        }
        Hero2 current = head.next;
        while(true){
            if (current == null) break;
            if (current.no == no) {
                flag = true;
                break;
            }
            current = current.next;
        }
        if (flag){
            if (current.pre != null) current.pre.next= current.next;
            if (current.next != null) current.next.pre = current.pre;
        }else{
            System.out.println("双向列表中不存在此英雄，删除失败！");
        }
    }
}
class Hero2{
    public  int no;
    public String name ;
    public String nickName;
    public Hero2 next;
    public Hero2 pre;
    //TODO   重写方法   如果加上 pre 会报 栈内存溢出错误
    @Override
    public String toString() {
        return "Hero2{" +
                "no=" + no +
                ", name='" + name + '\'' +
                ", nickName='" + nickName + '\'' +
                ", next=" + next +
                '}';
    }
    public Hero2(int no , String name, String nickName){
        this.no = no;
        this.name= name;
        this.nickName= nickName;
    }
}
